import { createRouter, createWebHistory } from 'vue-router'
import AlipayQrCode from "@/views/frontend/AlipayQrCode.vue";


const routes = [
  // 默认重定向到首页
  { path: '/', redirect: '/home' },
  // 认证相关路由
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Auth/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Auth/Register.vue'),
    meta: { title: '注册' }
  },
  // 前端用户页面（直接渲染）
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/frontend/Home.vue'),
    meta: { title: '首页' }
  },
  {
    path: '/course/list',
    name: 'CourseList',
    component: () => import('../views/frontend/CourseList.vue'),
    meta: { title: '课程列表' }
  },
  {
    path: '/course/detail/:id',
    name: 'CourseDetail',
    component: () => import('../views/frontend/CourseDetail.vue'),
    meta: { title: '课程详情' }
  },
  {
    path: '/course/learn/:id',
    name: 'CourseLearn',
    component: () => import('../views/frontend/CourseLearn.vue'),
    meta: { title: '课程学习', requiresAuth: true }
  },
  // 用户中心路由（直接渲染）
  {
    path: '/user/profile',
    name: 'UserProfile',
    component: () => import('../views/frontend/UserProfile.vue'),
    meta: { title: '个人信息', requiresAuth: true }
  },
  {
    path: '/user/orders',
    name: 'UserOrders',
    component: () => import('../views/frontend/UserOrders.vue'),
    meta: { title: '我的订单', requiresAuth: true }
  },
  {
    path: '/user/orders/create/:courseId',
    name: 'CreateOrder',
    component: () => import('../views/frontend/user/orders/CreateOrder.vue'),
    meta: { title: '创建订单', requiresAuth: true }
  },
  {
    path: '/user/courses',
    name: 'UserCourses',
    component: () => import('../views/frontend/UserCourses.vue'),
    meta: { title: '我的课程', requiresAuth: true }
  },
  // 管理员登录页面（直接渲染）
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: () => import('../views/admin/AdminLogin.vue'),
    meta: { title: '管理员登录' }
  },
  // 管理员后台页面（每个页面独立渲染）
  {
    path: '/admin/dashboard',
    name: 'AdminDashboard',
    component: () => import('../views/admin/Dashboard.vue'),
    meta: { title: '后台首页', requiresAdmin: true }
  },
  {
    path: '/admin/users',
    name: 'AdminUsers',
    component: () => import('../views/admin/Users.vue'),
    meta: { title: '用户管理', requiresAdmin: true }
  },
  {
    path: '/admin/course-types',
    name: 'AdminCourseTypes',
    component: () => import('../views/admin/CourseTypes.vue'),
    meta: { title: '课程分类管理', requiresAdmin: true }
  },
  {
    path: '/admin/courses',
    name: 'AdminCourses',
    component: () => import('../views/admin/Courses.vue'),
    meta: { title: '课程管理', requiresAdmin: true }
  },
  {
    path: '/admin/orders',
    name: 'AdminOrders',
    component: () => import('../views/admin/Orders.vue'),
    meta: { title: '订单管理', requiresAdmin: true }
  },
  {
    path: '/admin/employees',
    name: 'AdminEmployees',
    component: () => import('../views/admin/Employees.vue'),
    meta: { title: '员工管理', requiresAdmin: true }
  },
// 支付宝支付二维码页面
  {
    path: '/pay/alipay',
    name: 'AlipayQrCode',
    component: AlipayQrCode
  },
  // 404路由
  { path: '/:pathMatch(.*)*', redirect: '/home' }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }
  
  // 权限验证 - 用户权限（处理不同的meta名称）
  if (to.meta.requireAuth || to.meta.requiresAuth) {
    const token = localStorage.getItem('token')
    if (!token) {
      // 未登录，重定向到登录页
      next('/login')
      return
    }
  }
  
  // 权限验证 - 管理员权限（处理不同的meta名称）
  // 开发阶段完全跳过管理员权限验证
  if (to.meta.requireAdmin || to.meta.requiresAdmin) {
    // 始终允许访问管理员页面，不做登录验证
    // const adminToken = localStorage.getItem('adminToken')
    // if (!adminToken) {
    //   // 未登录，重定向到管理员登录页
    //   next('/admin/login')
    //   return
    // }
  }
  
  next()
})

export default router